﻿using System.Windows.Controls;
using FishSimulation.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace FishSimulation.Views {
    /// <summary>
    /// Interaction logic for ActorTypeGridView.xaml
    /// </summary>
    public partial class ActorTypeGridView : ViewBase {
        public ActorTypeGridView() {
            InitializeComponent();
            this.Bind();
        }

        private void btnBrowse_Click(object sender, System.Windows.RoutedEventArgs e) {
            if (dg.SelectedItem != null) {
                var actorType = dg.SelectedItem as ActorType;
                using (var cd = new System.Windows.Forms.ColorDialog()) {
                    cd.AllowFullOpen = true;
                    cd.FullOpen = true;
                    var result = cd.ShowDialog();
                    if (result != System.Windows.Forms.DialogResult.Cancel) {
                        var b = new System.Windows.Media.SolidColorBrush();
                        b.Color = new System.Windows.Media.Color(){ R = cd.Color.R, G = cd.Color.G, B = cd.Color.B, A = cd.Color.A };
                        actorType.Color = b;
                    }
                }
            }
        }

        private void btnFoodDecrementFormula_Click(object sender, System.Windows.RoutedEventArgs e) {
            var obj = dg.SelectedItem as ActorType;
            var win = Utils.CreateWindow();
            var view = new FormulaEditorView();
            view.ShowOtherPropreties = false;
            view.Formula = obj.FoodDecrementFormula;
            win.Content = view;
            win.ShowDialog();

            if (!view.Canceled) {
                obj.FoodDecrementFormula = view.Formula;
            }
        }

        private void btnFoodValueFormula_Click(object sender, System.Windows.RoutedEventArgs e) {
            var obj = dg.SelectedItem as ActorType;
            var win = Utils.CreateWindow();
            var view = new FormulaEditorView();
            view.Formula = obj.FoodValueFormula;
            win.Content = view;
            win.ShowDialog();

            if (!view.Canceled) {
                obj.FoodValueFormula = view.Formula;
            }
        }

        private void btnMenu_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var win = Utils.CreateWindow();
            var view = new ActorTypeMenuView();            
            win.Content = view;
            view.Bind(((Button)sender).Tag as IEnumerable<ActorType>);
            win.ShowDialog();
            
            var items = view.GetCheckedItems();
            ObservableCollection<ActorType> oitems = new ObservableCollection<ActorType>();
            foreach(var i in items)
                oitems.Add(i);
            (dg.SelectedItem as ActorType).Menu = oitems;
        }

        private void btnSave_Click(object sender, System.Windows.RoutedEventArgs e) {
            var l = dg.ItemsSource as IEnumerable<ActorType>;
            foreach (var item in l) {
                if (item.IsDirty) {                                        
                    Repository.Instance.Store(item);
                }
            }
        }

        private void btnWeightFormula_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var obj = dg.SelectedItem as ActorType;
            var win = Utils.CreateWindow();
            var view = new FormulaEditorView();
            view.ShowOtherPropreties = false;
            view.Formula = obj.WeightFormula;            
            win.Content = view;
            win.ShowDialog();

            if (!view.Canceled)
            {
                obj.WeightFormula = view.Formula;
            }
        }

        private void dg_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Delete)
            {
                if (dg.SelectedItems != null && this.Confirm("Really delete selected actor type(s)?"))
                {
                    List<ActorType> alist = new List<ActorType>();
                    foreach (var obj in dg.SelectedItems)
                    {
                        ActorType a = obj as ActorType;
                        if (a != null)
                        {
                            alist.Add(a);
                        }
                    }
                    Repository.Instance.Delete(alist.ToArray());
                }
            }
        }

        private void Bind() {
            dg.ItemsSource = Repository.Instance.GetActorTypes();
        }        
    }
}
